<!--
 * @Author: 梁青春
 * @Date: 2025-11-13 15:00:00
 * @LastEditTime: 2025-11-13 15:17:21
 * @LastEditors: 梁青春
-->
<template>
  <el-dialog v-model="visible" title="登记信息" width="73vw" append-to-body>
    <div class="patient-content">
      <div class="patient-item">
        <div class="title">
          患者信息
          <el-button text bg class="read-card" size="small">身份识别</el-button>
        </div>
        <PatientInfo ref="patientInfoRef" />
      </div>
      <div class="patient-item">
        <div class="title">
          住院信息
          <div class="sub-title">入院:2025年第1次/计第1次</div>
        </div>
        <HospitalizationInfo ref="hospitalizationInfoRef" />
      </div>
    </div>
    <template #footer>
      <div class="patient-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="submit">确认登记</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="RegisterAdmissionDialog">
import PatientInfo from './PatientInfo.vue'
import HospitalizationInfo from './HospitalizationInfo.vue'
import { registerAdmission } from '@/api/chargingWork/admissionRegistration'
import { normalizePatientAddresses } from './registerAdmissionUtils'

// 中文注释：显隐控制，父组件使用 v-model
const props = defineProps({ modelValue: { type: Boolean, default: false } })
const emit = defineEmits(['update:modelValue', 'submitted'])
const visible = computed({ get: () => props.modelValue, set: v => emit('update:modelValue', v) })

// 中文注释：子表单组件引用
const patientInfoRef = ref(null)
const hospitalizationInfoRef = ref(null)

// 中文注释：关闭弹窗
const close = () => {
  visible.value = false
}

// 中文注释：提交登记，内部完成表单校验、数据规范化与接口提交
const submit = async () => {
  const patientInfo = await patientInfoRef.value.exposeForm()
  if (!patientInfo) return
  const hospitalizationInfo = await hospitalizationInfoRef.value.exposeForm()
  if (!hospitalizationInfo) return
  // 规范化地址相关字段
  normalizePatientAddresses(patientInfo)
  const form = { patient: patientInfo, admission: hospitalizationInfo }
  const res = await registerAdmission(form)
  if (res?.code === 200) {
    emit('submitted', res.data)
    close()
  }
}

// 中文注释：对外暴露方法，便于测试与父组件调用
defineExpose({ submit, close })
</script>

<style scoped lang="scss">
@use '@/assets/styles/variables.module.scss' as *;
.patient-content {
  padding: $padding-md;
}
.patient-item {
  margin-bottom: $margin-md;
}
.title {
  height: 36px;
  line-height: 36px;
  border: solid $border-color;
  border-width: 1px 1px 0 1px;
  padding: 0 $padding-sm;
  display: flex;
  align-items: center;
}
.sub-title {
  font-size: $font-size-sm;
  margin-left: $margin-sm;
  display: inline-flex;
}
.read-card {
  margin-left: auto;
}
.patient-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
</style>
